﻿<?php
	if (empty($_GET['term'])) exit ;
	$q = strtolower($_GET["term"]);
	$q = strtoupper(substr($q, 0, 1)) . substr($q, 1, strlen($q) - 1);

	include 'config.php';
	$query = "SELECT id, descripcion FROM cie WHERE descripcion LIKE '" . $q . "%'  AND !ISNULL(id_rango_cie_fk)";
	
	try {
		$dbh = new PDO('mysql:host=' . $host . ';dbname=' . $db, $user, $password, $options);
		$codigos = array();
		$json = "[";
		$primero = 0;
		foreach($dbh->query($query) as $row) {
			if($primero==0){
				$json .= '{"label": "' . htmlspecialchars($row['descripcion']) . '", "value": "' . htmlspecialchars($row['id']) . '"}';
				$primero = 1;
			}
			else
			{
				$json .= ',{"label": "' . htmlspecialchars($row['descripcion']) . '", "value": "' . htmlspecialchars($row['id']) . '"}';
			}
		}
		$json .= "]";
		echo $json;
		$dbh = null;
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}
?>